/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-02-03 17:25:45
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-07-13 15:53:04
 */
'use strict';
define([
    './baseBean'
], function(
    BaseBean
) {
    function me() {
        BaseBean.apply(this, arguments);
        var self = this;
        this.queryUser = function(callback, errorCallback) {
            var params = {};
            if (window.localStorage.fmpuser) {
                if (callback) {
                    var result = JSON.parse(window.localStorage.fmpuser);
                    self._setUser(result);
                    callback(result, params);
                }
                return;
            }
            self.p('/user/current', params, function(result) {
                self._setUser(result);
                window.localStorage.fmpuser = JSON.stringify(result);
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }

        this._setUser = function(result) {
            window.sessionStorage.roleId = result.roleId + '';
            //格式化时区
            window.sessionStorage.timezoneName = result.timezoneName || '零时区';
            window.sessionStorage.timezoneId = result.timezoneId || 'GMT';
            var _time = window.sessionStorage.timezoneId.substr(3) || '0';
            var _h = parseInt(_time);
            var _m = Math.abs(Number(_time) % 1 * 60);
            var _timeFarmat = moment(_h + ':' + _m, 'HH:mm').format('HH:mm');
            _timeFarmat = _h >= 0 ? '+' + _timeFarmat : '-' + _timeFarmat;
            window.sessionStorage.timezoneValue = _timeFarmat;
            window.localStorage.setItem('language', result.language)
                //显示当前用户名称
            window.sessionStorage.username = result.username || '';
            //公司信息
            window.sessionStorage.orgId = result.orgId || '';

        }

        /**
         * { 是否内部美宣 }
         *
         * @method     isUidesigner
         */
        this.isUidesigner = function() {
            var roleIds = window.sessionStorage.roleId.split(',');
            return self._.contains(roleIds, "3");
        }

        /**
         * { 外部美宣 }
         *
         * @method     isOuteruidesigner
         */
        this.isOuteruidesigner = function() {
            var roleIds = window.sessionStorage.roleId.split(',');
            return self._.contains(roleIds, "6");
        }

        this.logout = function(callback, errorCallback) {
            var params = {};
            self.p('/logusers/logout', params, function(result) {
                if (callback) {
                    callback(result, params);
                }
            }, errorCallback);
        }

        /**
         * { 清空缓存 }
         *
         * @method     reset
         */
        this.reset = function() {
            localStorage.removeItem('fmpuser')
            localStorage.removeItem('language')
            sessionStorage.removeItem('timezoneName');
            sessionStorage.removeItem('timezoneId');
            sessionStorage.removeItem('timezoneValue');
            sessionStorage.removeItem('username');
            sessionStorage.removeItem('orgId');
            sessionStorage.removeItem('roleId');
        }
    };
    return new me();
});
